package com.ncloud.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.body.Result;
import com.ncloud.common.utils.ResultUtil;
import com.ncloud.system.entity.ComputerRoom;
import com.ncloud.system.service.impl.ComputerRoomServiceImpl;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by gaowei on 2017/7/27.
 */
@RestController
@RequestMapping("/sys/room")
public class ComputerRoomController {
	@Autowired
	private ComputerRoomServiceImpl computerRoomService;
    @GetMapping(value = "/list/{areaId}/{countryId}/{provinceId}/{cityId}")
    public Result<?> getComputerRoomInfo(HttpServletRequest request,@PathVariable("areaId") String areaId,@PathVariable("countryId") String countryId,
    		@PathVariable("provinceId") String provinceId,@PathVariable("cityId") String cityId){
    	JSONObject params = new JSONObject();
    	params.put("areaId", areaId);
    	params.put("countryId", countryId);
    	params.put("provinceId", provinceId);
    	params.put("cityId", cityId);
    	JSONArray infos = computerRoomService.getComputerRoomInfo(params);
		return ResultUtil.success(infos);
    }
    @PostMapping(value = "/add")
    public Result<?> add(HttpServletRequest request, @RequestBody ComputerRoom computerRoom){
    	Object userID = request.getAttribute("userID");
    	//JSONObject params = JSONObject.parseObject(param);
    	computerRoom.setCreateUser(Long.parseLong(userID.toString()));
    	computerRoom.setCreateDate(new Date());
    	int num = computerRoomService.insertComputerRoom(computerRoom);
		return ResultUtil.success(num);
    }
    @PutMapping(value = "/update")
    public Result<?> update(HttpServletRequest request, @RequestBody ComputerRoom computerRoom){
    	Object userID = request.getAttribute("userID");
    	computerRoom.setUpdateUser(Long.parseLong(userID.toString()));
    	computerRoom.setUpdateDate(new Date());
    	int num = computerRoomService.updateComputerRoom(computerRoom);
		return ResultUtil.success(num);
    }
    @DeleteMapping(value = "/delete/{roomId}")
    public Result<?> delete(HttpServletRequest request, @PathVariable("roomId") Integer roomId){
    	int num = computerRoomService.deleteComputerRoom(roomId);
		return ResultUtil.success(num);
    }
}
